package com.hack.validator;

import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 验证器自动配置类
 * 包含验证引擎、验证拦截器和管理控制器
 *
 * @author hack
 * @version 1.0
 * @date 2025/07/11
 */
@Configuration
@ConditionalOnClass(ValidationEngine.class)
@ConditionalOnProperty(prefix = "param.validator", name = "enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(ValidatorProperties.class)
public class ValidatorAutoConfiguration {

    @Autowired
    private ValidatorProperties properties;

    @PostConstruct
    public void init() {
        DBManager.initDatabase();
    }

    @Bean
    @ConditionalOnMissingBean
    public ValidationEngine validationEngine() {
        return new ValidationEngine(properties.getAppId());
    }

    @Bean
    @ConditionalOnMissingBean
    public ValidationInterceptor validationInterceptor(ValidationEngine validationEngine) {
        return new ValidationInterceptor(validationEngine);
    }

    @Bean
    @ConditionalOnMissingBean
    public AdminController adminController() {
        return new AdminController();
    }
}